//
//  ContactUsViewController.swift
//  koolcar
//
//  Created by xiantian on 2023/11/2.
//

import UIKit

class ContactUsViewController: BaseViewController {
    var segmentView: ContactSegmentView!
    var scrollView: UIScrollView!
    
    var carLineView: CarLineView!
    var storeLineView: CarLineView!
    var kefuLineView: CarLineView!
    var articleData: ArticleVerboseDataModel?
    var contactId: String = ""
    var titleStr: String = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navBar.title = self.titleStr
        self.navBar.wr_setBottomLineHidden(hidden: true)
        
        segmentView = ContactSegmentView.init(frame: CGRect.init(x: 0, y: NavBarHight, width: kScreenWidth, height: 54))
        segmentView.callBack = { [weak self] tag in
            UIView.animate(withDuration: 0.2) {
                self?.scrollView.contentOffset.x = kScreenWidth * CGFloat(tag)
            }
        }
        self.view.addSubview(segmentView)
        
        let tempHeight: CGFloat = kScreenHeight - segmentView.frame.maxY
        scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: segmentView.frame.maxY, width: kScreenWidth, height: tempHeight))
        scrollView.contentSize = CGSize.init(width: kScreenWidth * 3, height: tempHeight)
        scrollView.isPagingEnabled = true
        scrollView.isScrollEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        self.view.addSubview(scrollView)
        self.getListVerbose()
    }
    
    
    ///暂时选写定
    private func getListVerbose(){
        self.view.showLoadingHUDText()
        XTRequestCenter.shared.sendRequestMappableTargetObject(target: APIManager.getListVerbose(param: ["id": self.contactId]), objectType: ArticleVerboseResModel.self) { data in
            self.view.handleLoading()
            let resultObj = data as! ArticleVerboseResModel
            if resultObj.code == "1" {
                self.articleData = resultObj.data
                self.reloadData()
            }else{
                self.view.makeToast(message: resultObj.msg ?? "")
            }
        } error: { err in
            self.view.handleLoading()
            self.view.makeToast(message: err?.domain ?? "")
        }
    }
    
    ///装载数据
    private func reloadData(){
        let tempList = self.articleData?.article ?? []
        let titles = tempList.map{$0.title ?? ""}
        if titles.count == 1 {
            self.segmentView.isHidden = true
            self.segmentView.frame.size.height = 0
            self.scrollView.frame.origin.y = NavBarHight
            self.scrollView.frame.size.height = kScreenHeight - NavBarHight
        }else{
            self.segmentView.isHidden = false
            self.segmentView.frame.size.height = 54
            self.segmentView.setContactSegmentView(titles: titles)
            self.scrollView.frame.origin.y = self.segmentView.frame.maxY
            self.scrollView.frame.size.height = kScreenHeight - NavBarHight
        }
        
        for (pIndex,itemObj) in tempList.enumerated() {
            let offsetX = CGFloat(pIndex) * kScreenWidth
            let tempView = CarLineView.init(frame: CGRect.init(x: offsetX, y: 0, width: kScreenWidth, height: self.scrollView.frame.size.height))
            tempView.loadHtml(h5Str: itemObj.content ?? "")
            self.scrollView.addSubview(tempView)
        }
    }
}
